package socket.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
  
  private static final int RECEIVE_BUFFER = 10;
  private static final int SO_TIMEOUT = 0;
  private static final boolean REUSE_ADDR = false;
  private static final int BACK_LOG = 2;
  //client socket listen property on server endpoint:
  private static final boolean CLI_KEEPALIVE = false;
  private static final boolean CLI_OOB = false;
  private static final int CLI_REC_BUF = 20;
  private static final boolean CLI_REUSE_ADDR = false;
  private static final int CLI_SEND_BUF = 20;
  private static final boolean CLI_LINGER = true;
  private static final int CLI_LINGER_N = 0;
  private static final int CLI_TIMEOUT = 0;
  private static final boolean CLI_NO_DELAY = false;
  
  public static void main(String[] args) {
    ServerSocket server = null;
    
    try {
      // 创建 server socket
      server = new ServerSocket();
      // 绑定端口
      server.bind(new InetSocketAddress(9111), BACK_LOG);
      
      // 设置 server socket的参数
      server.setReceiveBufferSize(RECEIVE_BUFFER);
      server.setReuseAddress(REUSE_ADDR);
      server.setSoTimeout(SO_TIMEOUT);
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    System.out.println("server up use 9111");
    
    try {
      while (true) {
        Socket client = server.accept();  //阻塞的，没有 -1  一直卡着不动  accept(4,
        System.out.println("client port: " + client.getPort());
        
        // 设置 keepalive
        client.setKeepAlive(CLI_KEEPALIVE);
        client.setOOBInline(CLI_OOB);
        client.setReceiveBufferSize(CLI_REC_BUF);
        client.setReuseAddress(CLI_REUSE_ADDR);
        // 发送消息的 buffer
        client.setSendBufferSize(CLI_SEND_BUF);
        client.setSoLinger(CLI_LINGER, CLI_LINGER_N);
        // 表示 设置延时
        client.setSoTimeout(CLI_TIMEOUT);
        // tcp 是否开启延时
        client.setTcpNoDelay(CLI_NO_DELAY);
        
        new Thread(() -> {
          try {
            InputStream in = client.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            char[] chars = new char[1024];
            
            while (true) {
              int num = reader.read(chars);
              if (num > 0) {
                System.out.println("client read some data is :" + num + " val :" + new String(chars, 0, num));
              } else if (num == 0) {
                System.out.println("client readed nothing!");
              } else {
                System.out.println("client readed -1...");
                System.in.read();
                client.close();
                break;
              }
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }).start();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
